package com.cuit.riskassessment.action.staff;

import java.io.File;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.context.ServletContextAware;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import com.cuit.riskassessment.service.StaffInfoService;
import com.cuit.riskassessment.vo.StaffInfo;

@Controller
@RequestMapping(value = "staffInfoAction")
public class StaffInfoAction implements ServletContextAware {
	@Resource(name = "staffInfoServiceObject")
	private StaffInfoService service;
	private ServletContext servletContext;

	@Override
	public void setServletContext(ServletContext servletContext) {
		this.servletContext = servletContext;
	}

	@RequestMapping(value = "get")
	public @ResponseBody
	Object get(@RequestParam("name") String name,
			@RequestParam("val") String val,@RequestParam("start")String start,@RequestParam("limit")String limit)
			throws UnsupportedEncodingException {
		Map<String, Object> res=new HashMap<String, Object>();
		List<StaffInfo> list = new ArrayList<StaffInfo>();
		name = new String(name.getBytes("iso8859_1"), "utf-8");
		if (name == null || name.equals("")) {
			list =  service.getAll();
		} else if (name != null && val.equals("")) {
			list =  service.getByElement("VC_NAME", name);
		}else{
			list = service.getByElement(val, name);
		}
		res.put("totalcount", list.size());
		res.put("list", list);
		return res;
	}

	@RequestMapping(value = "addOrUpdate")
	public @ResponseBody
	void addOrUpdate(StaffInfo staffInfo) {
		if (staffInfo.getId() == null || staffInfo.getId().equals("")) {
			staffInfo.setCert("<a href = '../upload/" + staffInfo.getCert()
					+ "' target='_blank'>点击查看</a>");
			System.out.println(staffInfo.getCert());
			service.add(staffInfo);
		} else {
			service.update(staffInfo);
		}

	}

	@RequestMapping(value = "del")
	public @ResponseBody
	void del(String id) {
		if (service.getPhoto(id)!=null&&!(service.getPhoto(id).equals(""))) {
			String path = servletContext.getRealPath("/upload/photo");
			File file = new File(path+"\\"+service.getPhoto(id));
			if (file.isFile() && file.exists()) {
				file.delete();
			}
		}
		if (service.haveCert(id)!=null&&!(service.haveCert(id).equals(""))) {
			String littleName = service.haveCert(id);
			littleName = littleName.substring(21, littleName.length() - 10);
			String path = servletContext.getRealPath("/upload");
			File delFile = new File(path + "\\" + littleName);
			if (delFile.isFile() && delFile.exists()) {
				delFile.delete();
			}
		}
		service.del(id);
	}

	@RequestMapping(value = "photo")
	public @ResponseBody
	Object photo(HttpServletResponse response, @RequestParam("id") String id,
			@RequestParam("photoName") String photoName,
			@RequestParam("personPhoto") String personPhoto,
			@RequestParam("photo") CommonsMultipartFile photo) {
		response.setHeader("Content-Type", "text/html");

		if (photo.getSize() > 10000000) {
			return "{success:false}";
		}
		try {
			if (!photo.isEmpty()) {// 将文件存入服务器
				String path = servletContext.getRealPath("/upload/photo");
				File newFile = new File(path + "\\" + personPhoto);
				photo.getFileItem().write(newFile);
				if (id != null && !(id.equals(""))) {// 判断是新增还是修改
					StaffInfo staffInfo = new StaffInfo();
					staffInfo.setId(id);
					// 判断是否有文件 有文件就删除
					if (service.getPhoto(id) != null
							&& !(service.getPhoto(id).equals(""))) {
						String title = service.getPhoto(id);
						File deFile = new File(path + "\\" + title);
						if (deFile.isFile() && deFile.exists()) {
							deFile.delete();
						}
					}
					// 更新到数据库
					staffInfo.setPersonPhoto(personPhoto);
					service.updatePhoto(staffInfo);
				}
			}

		} catch (Exception e) {
			e.printStackTrace();
			return "{success:false}";
		}
		return "{success:true}";
	}

	@RequestMapping(value = "cert")
	public @ResponseBody
	String Cert(HttpServletResponse response, @RequestParam("id") String id,
			@RequestParam("cert") String cert,
			@RequestParam("code") CommonsMultipartFile code) {

		if (code.getSize() > 10000000) {// 判断文件大小
			return "{success:false}";
		}
		try {
			StaffInfo staffInfo = new StaffInfo();
			staffInfo.setId(id);
			// 将文件存入服务器
			if (!code.isEmpty()) {
				String path = servletContext.getRealPath("/upload");
				// 创建文件名
				File newfile = new File(path + "\\" + cert);
				code.getFileItem().write(newfile);// 写入文件
			}
			// 判断是新增还是删除
			if (id != null && !(id.equals(""))) {
				if (service.haveCert(id) == null
						|| service.haveCert(id).equals("")) {// 删除文件

				} else {
					String littleName = service.haveCert(id);
					littleName = littleName.substring(21, littleName.length()
							- "' target='_blank'>点击查看</a>".length());
					String path = servletContext.getRealPath("/upload");
					File delFile = new File(path + "\\" + littleName);
					if (delFile.isFile() && delFile.exists()) {
						delFile.delete();
					}
				}
				// 将路径添加到数据库
				String url = "<a href = '../upload/" + cert
						+ "' target='_blank'>点击查看</a>";
				staffInfo.setCert(url);
				service.updateCert(staffInfo);
				return "{success:true}";
			}
			// 判断服务器中是否有文件

		} catch (Exception e) {
			e.printStackTrace();
			return "{success:false}";
		}
		return "{success:true}";
	}

	@RequestMapping(value = "delFile")
	public @ResponseBody
	void delFile(@RequestParam("photo")String photo,@RequestParam("cert")String cert,@RequestParam("id")String id,@RequestParam("flag")String flag) {
		if (id!=null&&!(id.equals(""))) {
			if (flag.equals("0")) {
				if (photo!=null&&!(photo.equals(""))) {
					String path = servletContext.getRealPath("/upload/photo");
					File file = new File(path+"\\"+photo);
					if (file.isFile() && file.exists()) {
						file.delete();
					}
				}
				if (cert!=null&&!(cert.equals(""))) {
					String path = servletContext.getRealPath("/upload");
					File file = new File(path+"\\"+cert);
					if (file.isFile() && file.exists()) {
						file.delete();
					}
				}
				StaffInfo staffInfo = new StaffInfo();
				staffInfo.setId(id);
				staffInfo.setPersonPhoto("");
				staffInfo.setCert("");
				service.updatePhoto(staffInfo);
			}else if (flag.equals("1")) {
				if (cert!=null&&!(cert.equals(""))) {
					String path = servletContext.getRealPath("/upload");
					File file = new File(path+"\\"+cert);
					if (file.isFile() && file.exists()) {
						file.delete();
					}
					StaffInfo staffInfo = new StaffInfo();
					staffInfo.setId(id);
					staffInfo.setCert("");
					service.updatePhoto(staffInfo);
				}
			}else if (flag.equals("2")) {
				if (photo!=null&&!(photo.equals(""))) {
					String path = servletContext.getRealPath("/upload/photo");
					File file = new File(path+"\\"+photo);
					if (file.isFile() && file.exists()) {
						file.delete();
					}
				}
				StaffInfo staffInfo = new StaffInfo();
				staffInfo.setId(id);
				staffInfo.setPersonPhoto("");
				service.updatePhoto(staffInfo);
			}
		}else {
			if (photo!=null&&!(photo.equals(""))) {
				String path = servletContext.getRealPath("/upload/photo");
				File file = new File(path+"\\"+photo);
				if (file.isFile() && file.exists()) {
					file.delete();
				}
			}
			if (cert!=null&&!(cert.equals(""))) {
				String path = servletContext.getRealPath("/upload");
				File file = new File(path+"\\"+cert);
				if (file.isFile() && file.exists()) {
					file.delete();
				}
			}
		}
		
	}

}
